<template>
  <transition name="fade">
    <div v-if="show" :class="['message-toast', type]">
      {{ message }}
    </div>
  </transition>
</template>

<script>
export default {
  name: 'MessageToast',
  data() {
    return {
      show: false,
      message: '',
      type: ''
    };
  },
  methods: {
    // 显示正确提示
    showSuccess(msg) {
      this.message = msg;
      this.type = 'success';
      this.show = true;
      setTimeout(() => {
        this.show = false;
      }, 3000);
    },
    // 显示错误提示
    showError(msg) {
      this.message = msg;
      this.type = 'error';
      this.show = true;
      setTimeout(() => {
        this.show = false;
      }, 3000);
    }
  }
};
</script>

<style scoped>
.message-toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 20px;
  border-radius: 4px;
  z-index: 9999;
  color: white;
}

.message-toast.success {
  background-color: rgba(76, 175, 80, 0.9);
  /* 绿色背景表示成功 */
}

.message-toast.error {
  background-color: rgba(244, 67, 54, 0.9);
  /* 红色背景表示错误 */
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}
</style>
